<?php

/**
 * @author ducnhat
 * @copyright 2014
 */

/*========== WIDGET ===========*/
/* Recent Post Widget */
class DN_Recent_Posts_Widget extends WP_Widget {

	function __construct() {
		parent::__construct(
			'dn_recent_posts_widget', 
			__('DN Recent Posts', 'lskt'), 
			array( 'description' => __( 'DN Recent Posts Widget', 'lskt' )) 
		);
	}

	public function widget( $args, $instance ) {
		$title = apply_filters( 'widget_title', $instance['title'] );
        
		echo $args['before_widget'];
    		if ( ! empty( $title ) ){
                echo $args['before_title'] . $title . $args['after_title'];
    		}
   			
            $sliderCatID = get_theme_mod('home_setting_slider');
            $testimonialCatID = get_theme_mod('home_setting_testimonial');
            $portfolioCatID = get_theme_mod('home_setting_portfolio');
            
            $option = array('posts_per_page' => 4, 
                            'category__not_in' => array($sliderCatID, $testimonialCatID, $portfolioCatID)
                            );
            
            if($instance['category'] > 0){
                $option = array('category__in' => $instance['category']);
            }
            
    		$recentPosts = new WP_Query($option);
                ?>
                <div class="sidebar-detail">
                <ul class="recent-content">
                    <?php if($recentPosts->have_posts()): while($recentPosts->have_posts()): $recentPosts->the_post(); ?>
                    <?php global $post; ?>
                    <?php $thumb = get_post_meta($post->ID, 'thumbnail', true); ?>
                    <li>
                        <?php if(!empty($thumb)): ?>
                        <div class="recent-image">
                            <figure>
                                <a href="<?php the_permalink(); ?>">
                                    <div class="sidebar-thumbnail-hover"></div>
                                    <img width="65" src="<?php echo $thumb; ?>" alt="recent news" />
                                </a>
                            </figure>
                        </div>
                        <?php endif; ?>
                        <div class="recent-detail">
                            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                            <p><?php echo substr(get_the_content(' '), 0, 20) . "..."; ?></p>
                        </div>
                    </li>
                    <?php endwhile; ?>
                    <?php endif; ?>
                </ul>
                </div>
                <?php
            
		echo $args['after_widget'];
	}

	public function form( $instance ) {
		if ( isset( $instance[ 'title' ] ) ) {
			$title = $instance[ 'title' ];
		}
		else {
			$title = __( 'Recent Posts', 'lskt' );
		}
        
        if(isset($instance['number_posts'])){
            $number_posts = $instance['number_posts'];
        }else{
            $number_posts = 5;
        }
        
        if(isset($instance['category'])){
            $category = $instance['category'];
        }else{
            $category = 0;
        }
        
        $categories = get_categories();
		?>
		<p>
    		<label for="<?php echo $this->get_field_id( 'title' ); ?>">
                <?php _e( 'Title:' ); ?>
            </label> <br />
    		<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
		</p>
        <p>
            <label for="<?php echo $this->get_field_id( 'number_posts' ); ?>">
                <?php _e('Number Posts:') ?>
            </label><br />
            <input class="widefat" id="<?php echo $this->get_field_id('number_posts'); ?>" name="<?php echo $this->get_field_name('number_posts'); ?>" type="text" value="<?php echo $number_posts; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'category' ); ?>">
                <?php _e('Select Category:') ?>
            </label><br />
            <select class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name('category'); ?>">
                <option value="-1"><?php _e("Tất cả"); ?></option>
                <?php foreach($categories as $c): ?>
                    <option <?php if($c->term_id == $category) echo "selected='selected'"; ?> value="<?php echo $c->term_id; ?>"><?php echo $c->name; ?></option>
                <?php endforeach; ?>
            </select>
        </p>
		<?php 
	}

	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : $old_instance['title'];
        $instance['number_posts'] = ( ! empty( $new_instance['number_posts'] ) ) ? strip_tags( $new_instance['number_posts'] ) : $old_instance['number_posts'];
        $instance['category'] = ( ! empty( $new_instance['category'] ) ) ? strip_tags( $new_instance['category'] ) : $old_instance['category'];

		return $instance;
	}

} 

function register_dn_recent_posts_widget() {
    register_widget( 'DN_Recent_Posts_Widget' );
}
add_action( 'widgets_init', 'register_dn_recent_posts_widget' );
/* End Recent Post Widget */

/* Categories Widget */
class DN_Categories_Widget extends WP_Widget{
    public function DN_Categories_Widget(){
        parent::WP_Widget("dn_categories_widget", "DN Categories", array('description' => __("DN Categories Widget", "lskt")));
    }
    
    public function widget($args, $instance){
        $title = apply_filters('widget_tag', $instance['title']);
        
        echo $args['before_widget'];
        
        if(!empty($title)){
            echo $args['before_title'] . $title . $args['after_title'];
        }
        
        $excludeTestimonialCatID =  get_theme_mod('home_setting_testimonial');
        $excludeSliderCatID =  get_theme_mod('home_setting_slider');
        $portfolioCatID = get_theme_mod('home_setting_portfolio');
        
        $categories = get_categories("exclude=$excludeTestimonialCatID,$excludeSliderCatID,$portfolioCatID");
        ?>
        <div class="sidebar-detail sidebar-list">
            <ul>
                <?php foreach($categories as $category): ?>
                <li>
                    <i class="fa fa-angle-right"></i>
                    <a href="<?php echo get_category_link($category->cat_ID); ?>"><?php echo $category->name; ?></a>
                </li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php
        echo $args['after_widget'];
    }
    
    public function form($instance){
        if(isset($instance['title'])){
            $title = $instance['title'];
        }else{
            $title = "Danh mục";
        }
        
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">
                <?php _e("Title: ", "lskt") ?>
            </label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($title); ?>" />
        </p>
        <?php
    }
    
    public function update( $new_instance, $old_instance ) {
        parent::update( $new_instance, $old_instance );
 
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        return $instance;
	}
}

function register_dn_categories_widget(){
    register_widget("DN_Categories_Widget");
}
add_action('widgets_init', 'register_dn_categories_widget');
/* End Categories Widget */

/* Archives Widget */
class DN_Archives_Widget extends WP_Widget{
    
    public function __construct(){
    	parent::__construct("dn_archives_widget", "DN Archives Widget", array('description' => 'DN Archives Widget'));
    }
    
    public function widget($args, $instance){
    	$title = apply_filters('widget_tag', $instance['title']);
        
        echo $args['before_widget'];
        
        if(!empty($title)){
            echo $args['before_title'] . $title . $args['after_title'];
        }
        ?>
        <div class="sidebar-detail sidebar-list">
            <ul>
                <?php $archives = get_archives('', '12', '', '<li><i class="fa fa-angle-right"></i>', '</li>'); ?>
            </ul>
        </div>
        <?php
        echo $args['after_widget'];
    }
    
    public function form($instance){
    	if(isset($instance['title'])){
    	   $title = $instance['title'];
    	}else{
    	   $title = "Lưu trữ";
    	}
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">
                <?php echo __("Title:", "lskt") ?>
            </label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($title); ?>" />
        </p>
        <?php
    }
    
    public function update($new_instance, $old_instance){
    	$instane = $old_instance;
        
        $instane['title'] = strip_tags($new_instance['title']);
        
        return $instane;
    }
}

function register_dn_archives_widget(){
    register_widget("DN_Archives_Widget");
}
add_action('widgets_init', "register_dn_archives_widget");
/* ENd Archives Widget */

/* Archives Widget */
class DN_Newsletter_Widget extends WP_Widget{
    
    public function __construct(){
    	parent::__construct("dn_newsletter_widget", "DN NewsLetter Widget", array('description' => 'DN NewsLetter Widget'));
    }
    
    public function widget($args, $instance){
    	$title = apply_filters('widget_tag', $instance['title']);
        $description = apply_filters('widget_tag', $instance['description']);
        
        echo $args['before_widget'];
        
        if(!empty($title)){
            echo $args['before_title'] . $title . $args['after_title'];
        }
        ?>
        <div class="footer-details">
            <?php if(!empty($description)): ?>
            <p><?php echo $description; ?></p>
            <?php endif; ?>
            <div class="subscribe-box">
                <form target="_blank" method="post" action="http://ducnhat.us3.list-manage.com/subscribe/post">
                    <input type="hidden" value="422e6ae9dbbd7ef9685706fc0" name="u" />
                    <input type="hidden" value="4f7472c9cd" name="id" />
                    <input class="your-email" id="MERGE0" type="email" value="" size="25" name="MERGE0" autocorrect="off" autocapitalize="off" />
                    <input class="subscribe-button" type="submit" value="Subscribe"/>
                </form>
            </div>
        </div>
        <?php
        echo $args['after_widget'];
    }
    
    public function form($instance){
    	if(isset($instance['title'])){
    	   $title = $instance['title'];
    	}else{
    	   $title = "Newsletter";
    	}
        
        if(isset($instance['description'])){
    	   $description = $instance['description'];
    	}else{
    	   $description = "Đăng ký nhận tin mới qua email.";
    	}
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">
                <?php echo __("Title:", "lskt") ?>
            </label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($title); ?>" />
        </p>
        
        <p>
            <label for="<?php echo $this->get_field_id('description'); ?>">
                <?php echo __("Description:", "lskt") ?>
            </label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" value="<?php echo esc_attr($description); ?>" />
        </p>
        <?php
    }
    
    public function update($new_instance, $old_instance){
    	$instane = $old_instance;
        
        $instane['title'] = strip_tags($new_instance['title']);
        $instane['description'] = strip_tags($new_instance['description']);
        
        return $instane;
    }
}

function register_dn_newsletter_widget(){
    register_widget("DN_Newsletter_Widget");
}
add_action('widgets_init', "register_dn_newsletter_widget");
/* ENd Archives Widget */

/*========== END WIDGET ===========*/

?>